<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
  <div id="aaa">
    <span>fasafsaa</span>
  </div>
<script>

  document.getElementById("aaa").addEventListener("click",function(e){
    console.log(e.button)
  })
const isObject=(obj)=>{
  return  (typeof obj==="object")&&obj!==null;
}
const deepCopy = (obj, hash = new WeakMap()) => {
    if(!isObject(obj)){
      throw Error("不是一个对象")
    }
    if (hash.has(obj)) return hash.get(obj);
    let newObj=Array.isArray(obj)?[]:{};
    hash.set(obj,newObj);
    Object.keys(obj).forEach(v=>{
      if(isObject(obj[v])){
        newObj[v]=deepCopy(obj[v],hash)
      }else{
        newObj[v]=obj[v];
      }
    })
    return newObj
}
// 木易杨
function cloneDeep3(source, hash = new WeakMap()) {

if (!isObject(source)) return source; 
if (hash.has(source)) return hash.get(source); // 新增代码，查哈希表
  
var target = Array.isArray(source) ? [] : {};
hash.set(source, target); // 新增代码，哈希表设值

for(var key in source) {
    if (Object.prototype.hasOwnProperty.call(source, key)) {
        if (isObject(source[key])) {
            target[key] = cloneDeep3(source[key], hash); // 新增代码，传入哈希表
        } else {
            target[key] = source[key];
        }
    }
}
return target;
}
let copyObj={
  name:1,
  age:{
    list:[1],
    a:function(){
      console.log(1)
    }
  }
}
copyObj.ccc=copyObj;
console.log(deepCopy(copyObj))

</script>
  </body>
</html>